package com.jic.order.controller.manage;

import com.alibaba.fastjson.JSON;
import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.common.base.vo.enums.DeptTypeEnums;
import com.jic.order.entity.OrderAutomaticAftersale;
import com.jic.order.entity.OrderAutomaticCancel;
import com.jic.order.entity.OrderAutomaticComplete;
import com.jic.order.entity.OrderAutomaticReceipted;
import com.jic.order.manage.request.OrderInfoSetEditRequest;
import com.jic.order.manage.request.OrderInfoSetRequest;
import com.jic.order.manage.response.*;
import com.jic.order.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Api(value = "订单时效性设置", tags = {"订单时效性设置"})
@RequestMapping(value = "orderInfoSet")
@RestController
public class OrderInfoSetController {

	@Autowired
	OrderAutomaticCancelService orderAutomaticCancelService;
	@Autowired
	OrderAutomaticReceiptedService orderAutomaticReceiptedService;
	@Autowired
	OrderAutomaticCompleteService orderAutomaticCompleteService;
	@Autowired
	OrderAutomaticAftersaleService orderAutomaticAftersaleService;
	@Autowired
	OrderInfoSetService orderInfoSetService;

	@ApiOperation("获取订单实效设置信息")
	@ResponseBody
	@PostMapping(value = "/queryPage")
	public RestResult queryPage(@RequestBody OrderInfoSetRequest form) {
		log.info("开始调用订单实效设置分页，{}", JSON.toJSONString(form));
		DeptTypeEnums deptType = form.getAdminOperate().getDeptType();
		if (deptType.getCode() != 0) {
			form.setDeptId(form.getDeptId());
		}
		if (form.getTabType() == 1) {//订单自动取消
			PageResult<OrderAutomaticCancelResponse> result = this.queryOrderAutomaticCancel(form);
			return RestResult.success(result);
		} else if (form.getTabType() == 2) {//订单自动签收
			PageResult<OrderAutomaticReceiptedResponse> result = this.queryOrderAutomaticReceipted(form);
			return RestResult.success(result);
		} else if (form.getTabType() == 3) {
			PageResult<OrderAutomaticCompleteResponse> result = this.queryOrderAutomaticComplete(form);
			return RestResult.success(result);
		} else if (form.getTabType() == 4) {
			PageResult<OrderAutomaticAftersaleResponse> result = this.queryOrderAutomaticAftersale(form);
			return RestResult.success(result);
		} else {
			return RestResult.success(true);
		}

	}

	/**
	 * 订单自动取消
	 */
	public PageResult<OrderAutomaticCancelResponse> queryOrderAutomaticCancel(OrderInfoSetRequest form) {
		Page page = new Page();
		page.setLimit(form.getLimit());
		page.setStart(form.getStart());
		OrderAutomaticCancel orderAutomaticCancel = new OrderAutomaticCancel();
		PageResult<OrderAutomaticCancel> result = orderAutomaticCancelService.queryPage(orderAutomaticCancel, page);
		List<OrderAutomaticCancelResponse> orderAutomaticCancelResponseList = result.getRows().stream().map(temp -> {
			OrderAutomaticCancelResponse orderAutomaticCancelResponse = new OrderAutomaticCancelResponse();
			BeanUtils.copyProperties(temp, orderAutomaticCancelResponse);
			return orderAutomaticCancelResponse;
		}).filter(item -> item != null).collect(Collectors.toList());
		PageResult<OrderAutomaticCancelResponse> pageResult = new PageResult<>();
		pageResult.setRows(orderAutomaticCancelResponseList);
		pageResult.setTotal(result.getTotal());
		return pageResult;
	}

	/**
	 * 订单自动签收
	 */
	public PageResult<OrderAutomaticReceiptedResponse> queryOrderAutomaticReceipted(OrderInfoSetRequest form) {
		Page page = new Page();
		page.setLimit(form.getLimit());
		page.setStart(form.getStart());
		OrderAutomaticReceipted orderAutomaticReceipted = new OrderAutomaticReceipted();
		PageResult<OrderAutomaticReceipted> result = orderAutomaticReceiptedService.queryPage(orderAutomaticReceipted, page);
		List<OrderAutomaticReceiptedResponse> orderAutomaticReceiptedResponseList = result.getRows().stream().map(temp -> {
			OrderAutomaticReceiptedResponse orderAutomaticReceiptedResponse = new OrderAutomaticReceiptedResponse();
			BeanUtils.copyProperties(temp, orderAutomaticReceiptedResponse);
			return orderAutomaticReceiptedResponse;
		}).filter(item -> item != null).collect(Collectors.toList());
		PageResult<OrderAutomaticReceiptedResponse> pageResult = new PageResult<>();
		pageResult.setRows(orderAutomaticReceiptedResponseList);
		pageResult.setTotal(result.getTotal());
		return pageResult;
	}

	/**
	 * 订单自动完成
	 */
	public PageResult<OrderAutomaticCompleteResponse> queryOrderAutomaticComplete(OrderInfoSetRequest form) {
		Page page = new Page();
		page.setLimit(form.getLimit());
		page.setStart(form.getStart());
		OrderAutomaticComplete orderAutomaticComplete = new OrderAutomaticComplete();
		PageResult<OrderAutomaticComplete> result = orderAutomaticCompleteService.queryPage(orderAutomaticComplete, page);
		List<OrderAutomaticCompleteResponse> orderAutomaticCompleteResponseList = result.getRows().stream().map(temp -> {
			OrderAutomaticCompleteResponse orderAutomaticCompleteResponse = new OrderAutomaticCompleteResponse();
			BeanUtils.copyProperties(temp, orderAutomaticCompleteResponse);
			return orderAutomaticCompleteResponse;
		}).filter(item -> item != null).collect(Collectors.toList());
		PageResult<OrderAutomaticCompleteResponse> pageResult = new PageResult<>();
		pageResult.setRows(orderAutomaticCompleteResponseList);
		pageResult.setTotal(result.getTotal());
		return pageResult;
	}

	/**
	 * 订单自动审核表
	 */
	public PageResult<OrderAutomaticAftersaleResponse> queryOrderAutomaticAftersale(OrderInfoSetRequest form) {
		Page page = new Page();
		page.setLimit(form.getLimit());
		page.setStart(form.getStart());
		OrderAutomaticAftersale orderAutomaticAftersale = new OrderAutomaticAftersale();
		PageResult<OrderAutomaticAftersale> result = orderAutomaticAftersaleService.queryPage(orderAutomaticAftersale, page);
		List<OrderAutomaticAftersaleResponse> orderAutomaticAftersaleResponseList = result.getRows().stream().map(temp -> {
			OrderAutomaticAftersaleResponse orderAutomaticAftersaleResponse = new OrderAutomaticAftersaleResponse();
			BeanUtils.copyProperties(temp, orderAutomaticAftersaleResponse);
			return orderAutomaticAftersaleResponse;
		}).filter(item -> item != null).collect(Collectors.toList());
		PageResult<OrderAutomaticAftersaleResponse> pageResult = new PageResult<>();
		pageResult.setRows(orderAutomaticAftersaleResponseList);
		pageResult.setTotal(result.getTotal());
		return pageResult;
	}

	@ApiOperation("订单时效性设置  新增编辑")
	@ResponseBody
	@PostMapping(value = "/editOrderInfoSet")
	public RestResult editOrderInfoSet(@RequestBody OrderInfoSetEditRequest orderInfoSetEditRequest) {
		log.info("开始调用订单时效性设置新增编辑，{}", JSON.toJSONString(orderInfoSetEditRequest));
		RestResult restResult = new RestResult<>();
		try {
			restResult = orderInfoSetService.editOrderInfoSet(orderInfoSetEditRequest);
		} catch (Exception e) {
			log.error("editOrderInfoSet========》", e);
		}
		return restResult;

	}

	@ApiOperation("删除订单时效性设置")
	@ResponseBody
	@PostMapping(value = "/deleteOrderInfoSet")
	public RestResult deleteOrderInfoSet(@RequestBody OrderInfoSetRequest orderInfoSetRequest) {
		log.info("开始调用删除订单拆分规则，{}", orderInfoSetRequest.toString());
		RestResult restResult = new RestResult<>();
		try {
			orderInfoSetService.delete(orderInfoSetRequest);
			return RestResult.success(true);
		} catch (Exception e) {
			log.error("deleteOrderInfoSet========》", e);
		}
		return restResult;

	}

}
